package jmu.edu.yjc.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jmu.edu.yjc.Utils.BeanCopyUtil;
import jmu.edu.yjc.constants.SystemConstants;
import jmu.edu.yjc.domain.ResponseResult;
import jmu.edu.yjc.domain.entity.Comment;
import jmu.edu.yjc.domain.entity.User;
import jmu.edu.yjc.domain.vo.PageVo;
import jmu.edu.yjc.domain.vo.comment.CommentVo;
import jmu.edu.yjc.domain.vo.user.UserInfoVo;
import jmu.edu.yjc.mapper.CommentMapper;
import jmu.edu.yjc.service.CommentService;
import jmu.edu.yjc.service.user.UserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 评论表(Comment)表服务实现类
 *
 * @author makejava
 * @since 2023-04-14 00:53:01
 */
@Service("commentService")
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {
    @Resource
    private UserService userService;
    @Override
    public ResponseResult getRootList(Integer pageNum, Integer pageSize, Long aid) {
        LambdaQueryWrapper<Comment> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Comment::getCommentArticleId,aid);
        wrapper.eq(Comment::getIsRoot, SystemConstants.ROOT);
        wrapper.orderByDesc(Comment::getCreateTime);
        Page<Comment> page = new Page<>(pageNum,pageSize);
        page(page,wrapper);
        List<Comment> records = page.getRecords();
        List<CommentVo> commentVos = BeanCopyUtil.copyBeanList(records, CommentVo.class);
        for (CommentVo vo : commentVos) {
            User user = userService.getById(vo.getCreateBy());
            UserInfoVo userInfoVo = new UserInfoVo();
            userInfoVo.setId(vo.getCreateBy());
            if (user != null) {
                 userInfoVo = BeanCopyUtil.copyBeans(user, UserInfoVo.class);
            }
            vo.setCreateUser(userInfoVo);
        }
        wrapper.clear();
        wrapper.eq(Comment::getCommentArticleId,aid);
        return ResponseResult.okResult(new PageVo(commentVos, (long) count(wrapper)));
    }
    @Transactional
    @Override
    public ResponseResult addRootComment(Comment comment) {
        comment.setIsRoot(SystemConstants.ROOT);
        comment.setChildrenCount(0);
        save(comment);
        CommentVo commentvo = BeanCopyUtil.copyBeans(comment, CommentVo.class);
        User user = userService.getById(commentvo.getCreateBy());
        UserInfoVo createUser = new UserInfoVo();
        createUser.setId(commentvo.getCreateBy());
        if (user != null) {
            createUser = BeanCopyUtil.copyBeans(user, UserInfoVo.class);
        }
        commentvo.setCreateUser(createUser);
        return ResponseResult.okResult(commentvo);
    }

    @Override
    public ResponseResult getRootCommentMore(Integer commentNum, Integer commentSize,Long aid, Long rootCommentId) {
        LambdaQueryWrapper<Comment> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Comment::getCommentArticleId,aid);
        wrapper.eq(Comment::getRootCommentId,rootCommentId);
        Page<Comment> page = new Page<>(commentNum,commentSize);
        wrapper.orderByDesc(Comment::getCreateTime);
        page(page,wrapper);
        List<Comment> records = page.getRecords();
        List<CommentVo> commentVos = BeanCopyUtil.copyBeanList(records, CommentVo.class);
        for (CommentVo vo : commentVos) {
            User user = userService.getById(vo.getCreateBy());
            User toCommentUser = userService.getById(vo.getToCommentUserId());
            UserInfoVo createUser = new UserInfoVo();
            createUser.setId(vo.getCreateBy());
            UserInfoVo commentUser = new UserInfoVo();
            commentUser.setId(vo.getToCommentUserId());
            if(user != null ) {
                createUser = BeanCopyUtil.copyBeans(user, UserInfoVo.class);
            }
            if(toCommentUser != null) {
                commentUser = BeanCopyUtil.copyBeans(toCommentUser, UserInfoVo.class);
            }
            vo.setCreateUser(createUser);
            vo.setCommentUser(commentUser);
        }
        return ResponseResult.okResult(new PageVo(commentVos,page.getTotal()));
    }

    @Transactional
    @Override
    public ResponseResult addComment(Comment comment) {
        Comment rootComment = getById(comment.getRootCommentId());
        Integer count = rootComment.getChildrenCount();
        count = count + 1;
        rootComment.setChildrenCount(count);
        updateById(rootComment);
        save(comment);
        CommentVo commentvo = BeanCopyUtil.copyBeans(comment, CommentVo.class);
        User user = userService.getById(commentvo.getCreateBy());
        User toCommentUser = userService.getById(commentvo.getToCommentUserId());
        UserInfoVo createUser = new UserInfoVo();
        createUser.setId(commentvo.getCreateBy());
        UserInfoVo commentUser = new UserInfoVo();
        commentUser.setId(commentvo.getToCommentUserId());
        if(user != null ) {
            createUser = BeanCopyUtil.copyBeans(user, UserInfoVo.class);
        }
        if(toCommentUser != null) {
            commentUser = BeanCopyUtil.copyBeans(toCommentUser, UserInfoVo.class);
        }
        commentvo.setCreateUser(createUser);
        commentvo.setCommentUser(commentUser);
        return ResponseResult.okResult(commentvo);
    }

    @Transactional
    @Override
    public ResponseResult deleteChildrenComment(Long id) {
        Comment comment = getById(id);
        Comment rootComment = getById(comment.getRootCommentId());
        Integer count = rootComment.getChildrenCount();
        count = count - 1;
        rootComment.setChildrenCount(count);
        updateById(rootComment);
        removeById(id);
        return ResponseResult.okResult();
    }
}
